//
//  AppDelegate.h
//  TechGoldology
//
//  Created by 陈名邦 on 2018/9/29.
//  Copyright © 2018年 陈名邦. All rights reserved.
//
#import "TabbarController.h"
#import "HomeVC.h"
#import "MineVC.h"
#import "CoinVC.h"
#import "FCoinVC.h"
@interface TabbarController ()

@end

@implementation TabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.创建选项工具栏
    [self _createTabbarView];
    
    //2.创建子控制器
    [self _createViewControllers];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

//1.创建选项工具栏
- (void)_createTabbarView {
    
    // 按钮的图片数组
    NSArray *imageArray = @[@"形状6",@"矩形6",@"矩形5拷贝",@"形状7"];

    // 按钮的标题数组
    NSArray *titleArray = @[@"首页",@"币币",@"法币",@"我的"];

    
    // 按钮的宽、高
    CGFloat width = ScreenWidth / (float)titleArray.count;
    CGFloat height = self.tabBar.frame.size.height;
    
    for (int i = 0; i < titleArray.count; i++) {
        
        CGRect frame = CGRectMake(width * i, 0, width, height);
        //使用自定义的按钮样式
        TabbarItem *item = [[TabbarItem alloc] initWithFrame:frame
                                                   imageName:imageArray[i]
                                                       title:titleArray[i]];
        item.tag = i;
        if (i == 0) {
            item.isSelected = YES;
            _selectedItem = item;
        }
        
        [item addTarget:self action:@selector(selectAction:)
       forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:item];
    }
//    123213
}

//2.创建子控制器
- (void)_createViewControllers {
    
    //存放数组
    _controllers = [NSMutableArray array];
    
//    //首页
    HomeVC *know = [[HomeVC alloc]init];
    know.title = @"首页";
    UINavigationController *know_navi = [[UINavigationController alloc]initWithRootViewController:know];
    [_controllers addObject:know_navi];

    CoinVC *coin = [[CoinVC alloc]init];
    coin.title = @"币币";
    UINavigationController *coin_navi = [[UINavigationController alloc]initWithRootViewController:coin];
    [_controllers addObject:coin_navi];
    
    FCoinVC *fcoin = [[FCoinVC alloc]init];
    fcoin.title = @"法币";
    UINavigationController *fcoin_navi = [[UINavigationController alloc]initWithRootViewController:fcoin];
    [_controllers addObject:fcoin_navi];
   //我的
    MineVC *personVC = [[MineVC alloc] init];
    personVC.title = @"我的";
    UINavigationController *person_navi = [[UINavigationController alloc] initWithRootViewController:personVC];
    [_controllers addObject:person_navi];
    
    self.viewControllers = _controllers;
}

- (void)selectAction:(TabbarItem *)item {
    
    // 更改选中按钮的状态
    if (item != _selectedItem) {
        
        item.isSelected = YES;
        _selectedItem.isSelected = NO;
        _selectedItem = item;
        //跳转至对应的控制器
        self.selectedIndex = item.tag;
    }
}

//在视图将要展示时移除控制器自带的UITabBarButton
-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    NSArray *subViews = self.tabBar.subviews;
    for (UIView *view in subViews) {
        
        Class cla = NSClassFromString(@"UITabBarButton");
        //判断view对象是否是UITabBarButton类型
        if ([view isKindOfClass:cla]) {
            
            [view removeFromSuperview];
        }
    }
}


@end
